﻿/// <reference path="../../js/utils.js" />
'use strict';

define([
    'screenView',

    'common/models/travel.model',
    'common/models/travels.coll',

    'backgrid',
    
    'backgrid.extensions',
    'datepicker',
    'typeahead',
    'assets/backgrid/moment-cell/js/moment-cell',
],
function (ScreenView, TravelModel, TravelsColl, Backgrid, Typeahead) {
    var View = ScreenView.extend({
        super: null,

        name: 'driverTravels',
        title: 'Choferes/Viajes',

        initialize: function () {
            this.super = View.__super__;

            return this.super.initialize.apply(this, arguments);
        },


        render: function () {
            View.__super__.render.apply(this);

            /* First render actions. */
            if (!this.rendered) {
                var travelsColl = new TravelsColl();

                this.driverTravelsGrid = App.utils.grids.travels.driver(travelsColl);
                this.driverTravelsGrid.render();
                // Render the grid and attach the Grid's root to your HTML document
                this.$el.find('#travels').append(this.driverTravelsGrid.el);

                var travels = [
                    new TravelModel({ end: null, }),
                    new TravelModel({ driver: 'Mariano', }),
                    new TravelModel({ driver: 'Martin', }),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                ];

                travelsColl.set(travels);



                var drivers = [];
                for (var i = 0; i < travels.length; i++)
                {
                    var driver = travels[i].get('driver');

                    if (drivers.indexOf(driver) == -1)
                        drivers.push(driver);
                }

                this.$el.find('#searchItemTbx').typeahead({
                    source: drivers,
                });


                /* Indicates that the view was rendered at least one time. */
                this.rendered = true;
            }
            /* Not first render actions. */
            else {
                
            }


            return this;
        },
    });

    return View;
});